package reduce.fz.com.reduce.fragment.base;


import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.BaseListAdapter;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ReduceLog;

public abstract class BaseHorizontalFragment<VH extends BaseRecycleViewHolder,VC> extends Fragment {
    //Adapter
    public BaseListAdapter<VH,VC> baseListAdapter;

    public List<VC> datas;
    //上下文e
    private Context context;
    //分页
    protected Pagination pagination;

    private RecyclerView recyclerView;

    protected  LayoutInflater layoutInflater;
    private int layoutRid;


    public BaseHorizontalFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater,container,savedInstanceState);
        return null;
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.recyclerView=initRecycleView(view);
        initRecycleConfig();
        initListAdapter();
        this.recyclerView.setAdapter(this.baseListAdapter);
    }

    public abstract RecyclerView initRecycleView(View view);
    public abstract  VH createItemViewHolder(ViewGroup parent);
    public abstract  String createReqUrl();
    public void initListAdapter()
    {
        this.baseListAdapter=new BaseListAdapter<VH, VC>(context) {
            @Override
            public VH createFooterStatusViewHolder(View footerView) {
                return null;
            }

            @Override
            public VH createViewHolder(ViewGroup parent) {
                return createItemViewHolder(parent);
            }

            @Override
            public VH createHeaderHolder(ViewGroup parent) {
                return null;
            }
        };
    }

    public void nextPage()
    {
        String url=createReqUrl();
        url=url+pagination.nextPage();
        HttpUtils.get(url, new AbResponseListener<VC>(new TypeReference<Result<VC>>(){}) {
            @Override
            public void error(ServiceException e) {

            }

            @Override
            public void responseInfo(Result<VC> data) {
                if(!data.isSuccess()) return;

            }
        });

    }

    private   void initRecycleConfig()
    {
        // 设置ItemAnimator
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        recyclerView.setHasFixedSize(true);
    }







}
